// pages/dindanxingqi/dindanxingqi.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getdingdanUrl: app.data.url +'/mapi/newfenxiao/getteamproduct',
    title:[
      {
        name: '押金',
      },
      {
        name: '产品',
      }
    ],
    indexs:0,
    shuju:[
      [],
      [],
      []
    ],
    quanbu_page:1,
    yajin_page:1,
    canpin_page:1,
  },

  aa: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({indexs:index});
  },
  
  ae: function () {
    wx.navigateTo({
      url: '/pages/dindanxingqisan/dindanxingqisan',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getAllData();
    this.getYaJinData();
    this.getCanPinData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getAllData:function(){
    var that = this;
    var page = that.data.quanbu_page;
    if(page==0){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      });
      return;
    }
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data:{token:user.token,page:page},
      method:'post',
      success:function(res){
        var data = res.data;
        console.log(data)
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newdata = shuju[0];
          var array = data.data.data;
          that.setData({ quanbu_page:page+1})
          if(array.length<10){
            that.setData({quanbu_page:0});
          }
          ea.each(array,function(i,v){
            newdata.push(v);
          })
          shuju[0] = newdata;
          console.log(shuju);
          that.setData({ shuju: shuju });
          wx.hideLoading();
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  getYaJinData:function(){
    var that = this;
    var page = that.data.yajin_page;
    if(page==0){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
      return;
    }
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token, page: page ,type:1},
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data)
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newdata = shuju[0];
          var array = data.data.data;
          that.setData({ yajin_page: page + 1 })
          if(array.length<10){
            that.setData({yajin_page:0});
          }
          ea.each(array, function (i, v) {
            newdata.push(v);
          })
          shuju[0] = newdata;
          console.log(shuju);
          that.setData({ shuju: shuju });
          wx.hideLoading();
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  getCanPinData:function(){
    var that = this;
    var page = that.data.canpin_page;
    if(page==0){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
      return;
    }
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getdingdanUrl,
      data: { token: user.token, page: page, type: 2 },
      method: 'post',
      success: function (res) {
        var data = res.data;
        console.log(data)
        if (data.code == 200) {
          var shuju = that.data.shuju;
          var newdata = shuju[1];
          var array = data.data.data;
          that.setData({ canpin_page: page + 1 })
          if(array.length<10){
            that.setData({canpin_page:0})
          }
          ea.each(array, function (i, v) {
            newdata.push(v);
          })
          shuju[1] = newdata;
          console.log(shuju);
          that.setData({ shuju: shuju });
          wx.hideLoading();
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ shuju: [[], [], []], quanbu_page: 1, yajin_page: 1, canpin_page: 1, indexs:0});
    this.onLoad();
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var indexs = this.data.indexs;
    console.log(indexs);
    if(indexs==0){
      this.getYaJinData();
    }else if(indexs==1){
      this.getCanPinData();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})